package com.wang.sort3;

import com.wang.sort3.Demo08_GraphGenerator.Node;

import java.util.HashSet;
import java.util.Stack;

/**
 * @Author: along
 * @Create: 2021/4/23
 * @Description:图的深度优先遍历
 */
public class Demo10_DFS {
    public static void dfs(Node node) {
        if(node ==null) {
            return;
        }
        Stack<Node> stack = new Stack<>();
        HashSet<Node> set = new HashSet<>();
        stack.push(node);
        set.add(node);
        System.out.println(node.value);
        while (!stack.isEmpty()) {
            Node cur = stack.pop();
            for (Node next : cur.nexts
                 ) {
                if(!set.contains(next)) {//set中存在该node的其他next 说明已经遍历过该路径
                    stack.push(cur);
                    stack.push(next);
                    set.add(next);
                    System.out.println(next.value);
                    break;
                }
            }
        }
    }
}
